package com.vipheyue.bigcompany.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.dd.processbutton.iml.ActionProcessButton;
import com.vipheyue.bigcompany.R;
import com.vipheyue.bigcompany.domain.Company;
import com.vipheyue.bigcompany.utils.ProgressGenerator;
import com.vipheyue.bigcompany.view.CustomEditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.listener.SaveListener;

public class loginActivity extends AppCompatActivity implements ProgressGenerator.OnCompleteListener {

    public static final String EXTRAS_ENDLESS_MODE = "EXTRAS_ENDLESS_MODE";
    @Bind(R.id.et_userName)
    CustomEditText etUserName;
    @Bind(R.id.et_password)
    CustomEditText etPassword;
    @Bind(R.id.btnSignIn)
    ActionProcessButton btnSignIn;
    @Bind(R.id.free_regist_text_view)
    TextView freeRegistTextView;
    @Bind(R.id.forget_pwd_text_view)
    TextView forgetPwdTextView;
    private ProgressGenerator progressGenerator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        progressGenerator = new ProgressGenerator(this);
        btnSignIn.setMode(ActionProcessButton.Mode.ENDLESS);
    }

    @Override
    public void onComplete() {
    }

    @OnClick({R.id.btnSignIn, R.id.free_regist_text_view, R.id.forget_pwd_text_view})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnSignIn:
                progressGenerator.start(btnSignIn);
                btnSignIn.setEnabled(false);
                loginFunction();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        btnSignIn.setEnabled(true);
                    }
                },7*1000);
                break;
            case R.id.free_regist_text_view:
                Intent intent = new Intent(this, GetSMSActivity.class);
                intent.putExtra("type", "注册");
                startActivity(intent);
                break;
            case R.id.forget_pwd_text_view:
                Intent forgetIntent = new Intent(this, GetSMSActivity.class);
                forgetIntent.putExtra("type", "忘记密码");
                startActivity(forgetIntent);
                break;
        }
    }

    private void loginFunction() {
        Company user = new Company();
        user.setUsername(etUserName.getText().toString().trim());
        user.setPassword(etPassword.getText().toString().trim());
        user.login(this, new SaveListener() {
            @Override
            public void onSuccess() {
            startActivity(new Intent(loginActivity.this,MainActivity.class));
                finish();
            }
            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(loginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();

            }
        });
    }
}
